package org.openems;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.openems.meta.OtdrResultModelMeta;
import org.openems.model.OtdrResultModel;
import org.slim3.datastore.Datastore;

@Path("/jsontrace")
public class JsonTraceResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PrimitiveTraceEntity get(@QueryParam("key") String key, @QueryParam("points") String points) {
        OtdrResultModelMeta e = OtdrResultModelMeta.get();
        OtdrResultModel model = Datastore.query(e).asList().get(0);
        PrimitiveTraceEntity ret = new PrimitiveTraceEntity();
        double[] trace = model.getTrace();
        int max = Integer.valueOf(points);
        double step = (double)trace.length / (double)max;
        for (double i = 0; i < trace.length; i += step) {
            ret.y.add(trace[(int)i]);
        }
        ret.horizontalMax = (double)ret.y.size();
        ret.horizontalMin = 0.0;
  
        return ret;
    }
}
